<?php 
class CategoryModel extends Model{
	public $table='category';

	public $validate=array(
		array('catname','nonull','栏目不能为空',2,3),
		array('catname','checkCatname','栏目名已经存在',2,3)
	);

	public function checkCatname($name,$value,$msg,$arg){
		$data = M('category')->where("catname='$value'")->find();
		if($cid=Q('cid')){
			return empty($data) || $data['cid']==$cid?true:$msg;
		}else{
			return $data?$msg:True;
		}	
	}

	public function getList(){
		$data=$this->all();
		return Data::tree($data,'catname','cid','pid');
	}	

	public function addCategory(){
		if($this->create()){
			if($this->add()){
				return true;
			}else{
				$this->error='栏目添加失败';
				return false;
			}
		}
	}

	public function editCategory(){
		if($this->create()){
			if($this->where("cid={$_GET['cid']}")->update()){
				return true;
			}else{
				$this->error='栏目编辑失败';
				return false;
			}
		}
	}

	public function deleteCategory($cid){
		$isChannel=Data::channelList($this->all(),$cid);
		$categoryData=array_keys($isChannel);
		$categoryData[]=$cid;
		$map['cid']=array('IN',$categoryData);
		return $this->where($map)->del();
	}



}


?>